package com.tangruojie.stream;

import com.tangruojie.entity.Author;

import java.util.*;
import java.util.stream.Stream;

/**
 * 创建 Stream 流
 */
public class StreamDemo01 {

    public static void main(String[] args) {

        /**
         * 单列集合（List）
         */
        List<Author> authors = StreamDemoTest.getAuthors();
        Stream<Author> listStream = authors.stream();
        listStream.forEach(author -> System.out.println(author.getName()));
        System.out.println("------------------------------------------------------------------------------------------");

        /**
         * 数组
         */
        String[] arr = {"hello", "world", "hello world"};
        Stream<String> arrStream1 = Arrays.stream(arr);
        arrStream1.forEach(System.out::println);
        Stream<String> arrStream2 = Stream.of(arr);
        arrStream2.forEach(System.out::println);
        System.out.println("------------------------------------------------------------------------------------------");

        /**
         * 双列集合（Map）
         */
        HashMap<String, Integer> map = new HashMap<>();
        map.put("hello", 1);
        map.put("world", 2);
        map.put("hello world", 3);
        /**
         * 双列集合转化为单列集合（Set）
         */
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Stream<Map.Entry<String, Integer>> stream = entries.stream();
        stream.filter(entry -> entry.getValue() > 1).forEach(entry -> System.out.println(entry.getKey() + ":" + entry.getValue()));
        System.out.println("------------------------------------------------------------------------------------------");

        // 将所有 key 变成 List<String>
        List<String> keyList = map.keySet()
                .stream()
                .toList();
        keyList.forEach(System.out::println);

        // 将所有 value 变成 List<Integer>
        List<Integer> valueList = map.values()
                .stream()
                .toList();
        valueList.forEach(System.out::println);

    }

}
